/**
 * @Author 韦建韩
 * @Date 2022/5/20
 */
package com.example.devops.service.impl;

import com.example.devops.dao.PermissionMapper;
import com.example.devops.dao.UserMapper;
import com.example.devops.entity.common.Result;
import com.example.devops.entity.pojo.PrivilegeManagement;
import com.example.devops.entity.pojo.User;
import com.example.devops.entity.pojo.UserToPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionService {
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    UserMapper userMapper;

    public Result getPermission(){
        List<PrivilegeManagement> privilegeManagements = permissionMapper.selectList(null);
        return Result.doCreate(privilegeManagements);
    }
    public Result userToPermission(UserToPermission userToPermission){
        Long[] numberIds = userToPermission.getNumberIds();
        String permissionId = userToPermission.getPermissionId();
        PrivilegeManagement privilegeManagement = permissionMapper.selectById(permissionId);
        String number = privilegeManagement.getNumber();
        StringBuilder b = new StringBuilder(number);
        for (Long numberId : numberIds) {
             b.append(",");
            User user = userMapper.selectById(numberId);
            user.setPrivilege(user.getPrivilege()+permissionId);
            b.append(user.getUserName());
            userMapper.updateById(user);
        }
        privilegeManagement.setNumber(b.toString());
        permissionMapper.updateById(privilegeManagement);
        return Result.doCreate();
    }

}
